package cn.codeforfun.export;

import cn.codeforfun.consts.ExportConst;
import cn.codeforfun.exchage.Exchange;
import cn.codeforfun.exchage.ExchangeService;
import cn.codeforfun.follower.Follower;
import cn.codeforfun.follower.FollowerService;
import cn.codeforfun.prize.Prize;
import cn.codeforfun.prize.PrizeService;
import cn.codeforfun.vote.Option;
import cn.codeforfun.vote.Question;
import cn.codeforfun.vote.Vote;
import cn.codeforfun.vote.VoteService;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/2/18 0018.
 */
@Service
@Transactional
public class ExportService {
    @Autowired
    private ExchangeService exchangeService;
    @Autowired
    private VoteService voteService;
    @Autowired
    private PrizeService prizeService;
    @Autowired
    private FollowerService followerService;

    public Map list(String type) {
        Map map = new HashMap();
        if (ExportConst.EXCHANGE.equals(type)) {
            //兑换数据
            List<Exchange> exchangeList = exchangeService.findWithFollowerAndPrize();
            map.put("dataList", exchangeList);
        }
        if (ExportConst.PRIZE.equals(type)) {
            //奖品数据
            List<Prize> prizes = prizeService.findAll();
            map.put("dataList", prizes);
        }
        if (ExportConst.FOLLOWER.equals(type)) {
            //用户数据
            List<Follower> followers = followerService.findAll();
            map.put("dataList", followers);
        }
        if (ExportConst.VOTE.equals(type)) {
            //投票数据
            List<Vote> votes = voteService.list();
            map.put("dataList", votes);
        }
        return map;
    }

    public HSSFWorkbook export(String type) throws IOException {
        if (ExportConst.EXCHANGE.equals(type)) {
            //兑换数据
            List<Exchange> exchangeList = exchangeService.findWithFollowerAndPrize();
            return exportExchangeExcel(exchangeList);
        }
        if (ExportConst.PRIZE.equals(type)) {
            //奖品数据
            List<Prize> prizes = prizeService.findAll();
            return exportPrizeExcel(prizes);
        }
        if (ExportConst.FOLLOWER.equals(type)) {
            //用户数据
            List<Follower> followers = followerService.findAll();
            return exportFollowerExcel(followers);
        }
        if (ExportConst.VOTE.equals(type)) {
            //投票数据
            List<Vote> votes = voteService.list();
            return exportVoteExcel(votes);
        }
        return null;
    }

    public HSSFWorkbook exportExchangeExcel(List<Exchange> list) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("new sheet");
        HSSFRow row0 = sheet.createRow((short) 0);
        row0.createCell(0).setCellValue("奖品类型");
        row0.createCell(1).setCellValue("奖品详情");
        row0.createCell(2).setCellValue("奖品数量");
        row0.createCell(3).setCellValue("中奖几率");
        row0.createCell(4).setCellValue("中奖者微信名");
        row0.createCell(5).setCellValue("中奖者国家");
        row0.createCell(6).setCellValue("中奖者省份");
        row0.createCell(7).setCellValue("中奖者城市");
        row0.createCell(8).setCellValue("中奖者性别");
        row0.createCell(9).setCellValue("中奖者关注时间");
        row0.createCell(10).setCellValue("是否兑换");
        row0.createCell(11).setCellValue("中奖码");
        int j = 1;
        for (int i = 0; i < list.size(); i++) {
            Exchange exchange = list.get(i);
            HSSFRow row = sheet.createRow((short) j);
            row.createCell(0).setCellValue(exchange.getPrize().getTitle());
            row.createCell(1).setCellValue(exchange.getPrize().getDescription());
            row.createCell(2).setCellValue(exchange.getPrize().getTotal());
            row.createCell(3).setCellValue(exchange.getPrize().getRisk() + "%");
            row.createCell(4).setCellValue(exchange.getFollower().getNickname());
            row.createCell(5).setCellValue(exchange.getFollower().getCountry());
            row.createCell(6).setCellValue(exchange.getFollower().getProvince());
            row.createCell(7).setCellValue(exchange.getFollower().getCity());
            row.createCell(8).setCellValue(exchange.getFollower().getSex());
            Long longTime = exchange.getFollower().getSubscribeTime();
            String subStr = sdf.format(new Date(longTime * 1000L));
            row.createCell(9).setCellValue(subStr);
            row.createCell(10).setCellValue(exchange.getHasGet() == null || !exchange.getHasGet() ? "未兑换" : "已兑换");
            row.createCell(11).setCellValue(exchange.getCode());
            j++;
        }
        return wb;
    }

    public HSSFWorkbook exportPrizeExcel(List<Prize> list) {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("new sheet");
        HSSFRow row0 = sheet.createRow((short) 0);
        row0.createCell(0).setCellValue("奖品类型");
        row0.createCell(1).setCellValue("奖品详情");
        row0.createCell(2).setCellValue("奖品数量");
        row0.createCell(3).setCellValue("奖品剩余数量");
        row0.createCell(4).setCellValue("中奖几率");
        int j = 1;
        for (int i = 0; i < list.size(); i++) {
            Prize prize = list.get(i);
            HSSFRow row = sheet.createRow((short) j);
            row.createCell(0).setCellValue(prize.getTitle());
            row.createCell(1).setCellValue(prize.getDescription());
            row.createCell(2).setCellValue(prize.getTotal());
            row.createCell(3).setCellValue(prize.getUnExchanged());
            row.createCell(4).setCellValue(prize.getRisk() + "%");
            j++;
        }
        return wb;
    }

    public HSSFWorkbook exportFollowerExcel(List<Follower> list) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("new sheet");
        HSSFRow row0 = sheet.createRow((short) 0);
        row0.createCell(0).setCellValue("昵称");
        row0.createCell(1).setCellValue("性别");
        row0.createCell(2).setCellValue("国家");
        row0.createCell(3).setCellValue("省份");
        row0.createCell(4).setCellValue("城市");
        row0.createCell(5).setCellValue("关注时间");
        int j = 1;
        for (int i = 0; i < list.size(); i++) {
            Follower follower = list.get(i);
            HSSFRow row = sheet.createRow((short) j);
            row.createCell(0).setCellValue(follower.getNickname());
            row.createCell(1).setCellValue(follower.getSex());
            row.createCell(2).setCellValue(follower.getCountry());
            row.createCell(3).setCellValue(follower.getProvince());
            row.createCell(4).setCellValue(follower.getCity());
            Long longTime = follower.getSubscribeTime();
            String subStr = longTime == null ? "" : sdf.format(new Date(longTime * 1000L));
            row.createCell(5).setCellValue(subStr);
            j++;
        }
        return wb;
    }

    public HSSFWorkbook exportVoteExcel(List<Vote> list) throws IOException {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("new sheet");
        HSSFRow row0 = sheet.createRow((short) 0);
        row0.createCell(0).setCellValue("投票标题");
        row0.createCell(1).setCellValue("投票详情");
        row0.createCell(2).setCellValue("投票人数");
        row0.createCell(3).setCellValue("问题");
        row0.createCell(4).setCellValue("选项");
        row0.createCell(5).setCellValue("投票数");
        row0.createCell(6).setCellValue("百分比");
        int j = 1;
        for (int i = 0; i < list.size(); i++) {
            Vote vote = list.get(i);
            for (Question question : vote.getQuestions()) {
                for (Option option : question.getOptions()) {
                    HSSFRow row = sheet.createRow((short) j);
                    row.createCell(0).setCellValue(vote.getTitle());
                    row.createCell(1).setCellValue(vote.getDescription());
                    row.createCell(2).setCellValue(vote.getAmount());
                    row.createCell(3).setCellValue(question.getTitle());
                    row.createCell(4).setCellValue(option.getTitle());
                    row.createCell(5).setCellValue(option.getAmount());
                    Double total = question.getAmount() == 0 ? 0.00 : new BigDecimal(option.getAmount()).divide(new BigDecimal(question.getAmount()), 4, RoundingMode.HALF_UP).doubleValue();
                    row.createCell(6).setCellValue(total * 100 + "%");
                    j++;
                }
            }
        }
        return wb;
    }
}
